define(['jquery', 'bootstrap', 'backend', 'addtabs', 'table', 'echarts', 'echarts-theme', 'template'], function ($, undefined, Backend, Datatable, Table, Echarts, undefined, Template) {

    var Controller = {
        index: function () {
			require(['bootstrap-datetimepicker'], function () {
                var options = {
					format: 'YYYY',
					icons: {
						time: 'fa fa-clock-o',
						date: 'fa fa-calendar',
						up: 'fa fa-chevron-up',
						down: 'fa fa-chevron-down',
						previous: 'fa fa-chevron-left',
						next: 'fa fa-chevron-right',
						today: 'fa fa-history',
						clear: 'fa fa-trash',
						close: 'fa fa-remove'
					},
					showTodayButton: true,
					showClose: true
				};
				$('.datetimepicker').datetimepicker(options).on('dp.change', function (e) {
					$(this, document).trigger("changed");
				}).attr('disabled', false);
			});
			
			require(['selectpage'], function () {
				$('.selectpage').selectPage({
					eAjaxSuccess: function (data) {
						data.list = typeof data.rows !== 'undefined' ? data.rows : (typeof data.list !== 'undefined' ? data.list : []);
						data.totalRow = typeof data.total !== 'undefined' ? data.total : (typeof data.totalRow !== 'undefined' ? data.totalRow : data.list.length);
						return data;
					}
				});
			});
			
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    developer_url: 'dashboard/developer' + location.search,
                    company_url: 'dashboard/company' + location.search,
					clue_url: 'dashboard/clue' + location.search,
                    table: 'clue',
                }
            });

            var developer_table = $("#developer_table");
            // 初始化表格
            developer_table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.developer_url,
                pk: 'id',
                sortName: 'xscount',
				search: false,
				commonSearch: false,
				showToggle: false,
				showExport: false,
				showColumns: false,
                columns: [
                    [
                        {field: 'developtime', title: __('统计时间'), formatter: function(value, row, index){
							var tjtime = '';
							var year = Config.year;
							if(year)
							{
								tjtime = year+'年';
							}
							
							var month = Config.month;
							if(month)
							{
								tjtime += month+'月';
							}
							return tjtime;
						}},
						{field: 'admin.nickname', title: __('开发人')},
						{field: 'xscount', title: __('线索数'), sortable:true},
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(developer_table);
			
			var company_table = $("#company_table");
            // 初始化表格
            company_table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.company_url,
                pk: 'id',
                sortName: 'xscount',
				search: false,
				commonSearch: false,
				showToggle: false,
				showExport: false,
				showColumns: false,
                columns: [
                    [
						{field: 'developtime', title: __('统计时间'), formatter: function(value, row, index){
							var tjtime = '';
							var year = Config.year;
							if(year)
							{
								tjtime = year+'年';
							}
							
							var month = Config.month;
							if(month)
							{
								tjtime += month+'月';
							}
							return tjtime;
						}},
                        {field: 'companyname', title: __('公司名称')},
                        {field: 'xscount', title: __('线索数'), sortable:true},
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(company_table);
			
			var clue_table = $("#clue_table");
            // 初始化表格
            clue_table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.clue_url,
                pk: 'id',
                sortName: 'developtime',
				sortOrder: 'asc',
				search: false,
				commonSearch: false,
				searchFormVisible: false,
				showToggle: false,
				showExport: false,
				showColumns: false,
                columns: [
                    [
						{field: 'companyname', title: __('公司名称'), searchable:false},
						{field: 'admin', title: __('开发人')},
						{field: 'developtime', title: __('未有线索天数'), sortable:true, formatter: function(value, row, index){
							var current_timestamp = new Date().getTime()/1000;
							var days = Math.floor((current_timestamp-value)/86400);
							return days;
						}},
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(clue_table);
        }
    };

    return Controller;
});
